package com.ruoyi.merchant.controller;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.merchant.domain.BusinessInfo;
import com.ruoyi.merchant.domain.MerchantImgs;
import com.ruoyi.merchant.domain.MerchantVerification;
import com.ruoyi.merchant.service.IMerchantImgsService;
import com.ruoyi.merchant.service.IMerchantVerificationService;
import com.ruoyi.merchant.util.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.merchant.domain.Merchant;
import com.ruoyi.merchant.service.IMerchantService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商户Controller
 *
 * @author ruoyi
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/merchant")
public class MerchantController extends BaseController {
    @Autowired
    private IMerchantService merchantService;
    @Autowired
    private IMerchantImgsService merchantImgsService;
    @Autowired
    private IMerchantVerificationService merchantVerificationService;

    /**
     * 查询商户列表
     */
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/list")
    public TableDataInfo list(Merchant merchant) {
        startPage();
        List<Merchant> list = merchantService.selectMerchantList(merchant);
//        SMSUtil.send("15837524281", "code:123456");
//        System.out.println("发送成功");
        return getDataTable(list);
    }

    @PostMapping("/merchantlist")
    @InnerAuth
    public TableDataInfo merchantlist(@RequestBody Merchant merchant) {
        startPage();
        List<Merchant> list = merchantService.selectMerchantList(merchant);

        return getDataTable(list);
    }

    /**
     * 导出商户列表
     */
    @RequiresPermissions("merchant:merchant:export")
    @Log(title = "商户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Merchant merchant) {
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        ExcelUtil<Merchant> util = new ExcelUtil<Merchant>(Merchant.class);
        util.exportExcel(response, list, "商户数据");
    }

    /**
     * 获取商户详细信息
     */
    @RequiresPermissions("merchant:merchant:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(merchantService.selectMerchantById(id));
    }

    /**
     * 新增商户
     */
    @RequiresPermissions("merchant:merchant:add")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Merchant merchant) {
        System.out.println(merchant);
        merchant.setRegTime(new Date());
        merchantService.insertMerchant(merchant);
        return AjaxResult.success(merchant.getId());
    }

    @InnerAuth
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping("merchantAdd")
    public AjaxResult merchantAdd(@RequestBody Merchant merchant) {
        System.out.println(merchant);
        merchant.setRegTime(new Date());
        merchantService.insertMerchant(merchant);
        return AjaxResult.success(merchant.getId());
    }

    /**
     * 修改商户
     */
    @RequiresPermissions("merchant:merchant:edit")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Merchant merchant) {
        return toAjax(merchantService.updateMerchant(merchant));
    }

    /**
     * 删除商户
     */
    @RequiresPermissions("merchant:merchant:remove")
    @Log(title = "商户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        return toAjax(merchantService.removeBatchByIds(list));
    }

    /**
     * 回收商户
     */
//    @RequiresPermissions("merchant:merchant:edit")
//    @Log(title = "商户", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public AjaxResult recycle(@PathVariable Long ids)
//    {
//        Merchant merchant = new Merchant();
//        merchant.setId(ids);
//        long ss=0;
//        merchant.setDel(ss);
//        return toAjax(merchantService.updateMerchant(merchant));
//    }
    /*商户排名*/
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/merchantorderbyscore")
    public TableDataInfo merchantorderbyscore() {
        startPage();
        List<Map> maps = merchantService.selectMerchantOrderByscore();

        return getDataTable(maps);
    }

    @GetMapping("/phoneCode")
    public String phoneCode(@RequestParam("phone") String phone) {
        String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        SMSUtil.send(phone, "code:" + code);
        System.out.print("发送成功" + code);
        return code;
    }

    @RequiresPermissions("merchant:merchant:edit")
    @PostMapping("/editinfor")
    public AjaxResult editinfor(@RequestBody BusinessInfo businessInfo) {
//        System.out.print(businessInfo);
        Merchant merchant = new Merchant();
        merchant.setId(Long.valueOf(businessInfo.getMid()));
        merchant.setName(businessInfo.getName());
        merchant.setOperatingArea(businessInfo.getOperatingArea());
        merchant.setAddress(businessInfo.getAddress());
        merchant.setDescription(businessInfo.getDescription());
        merchant.setAuditStatusId(0L);
        merchantService.updateMerchant(merchant);
        MerchantVerification merchantVerification = new MerchantVerification();
        merchantVerification.setId(businessInfo.getUserid());
        merchantVerification.setDirector(businessInfo.getDirector());
        merchantVerification.setIdCard(businessInfo.getIdCard());
        merchantVerification.setBusinessLicenseId(businessInfo.getBusinessLicenseId());
        merchantVerification.setBusinessLicenseImg(businessInfo.getBusinessLicenseImg());
        merchantVerification.setFront(businessInfo.getFront());
        merchantVerification.setOpposite(businessInfo.getOpposite());
        merchantVerificationService.updateMerchantVerification(merchantVerification);
        MerchantImgs[] merchantImgsList = new MerchantImgs[9];
        for (String s : businessInfo.getFacadePhoto()) {
            if (businessInfo.getFacadePhoto().indexOf(s) > 8) {
                break;
            }
            MerchantImgs merchantImgs = new MerchantImgs();
            merchantImgs.setMid(Long.valueOf(businessInfo.getMid()));
            merchantImgs.setImg(s);

            merchantImgsList[businessInfo.getFacadePhoto().indexOf(s)] = merchantImgs;
        }
        merchantImgsService.updateMerchantImgs(merchantImgsList);
        return success();
    }
}
